package Default;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import world.Time;


public class Agent {

	int ID;
	Point position;
	ArrayList<DataMessage> data_list=null;
	ArrayList<ArrayList<DataMessage>> recieve_data=null;
	ArrayList<ArrayList<DataMessage>> recieve_data_new=null;
	ArrayList<DataMessage> tmp_data=null;
	//static Environment envi=null;
	//static int sense_radius;
	double battery;
	Connect connect=null;
	public double senseVol=0;
	
	public double childDepth=0;
	public Color myColor;
	public boolean sleep=false;
	
	
	public static int cntTemp=0;
	
	public Agent(int id,int x,int y,double _energy)
	{
		//if(envi==null)
		//	envi=env;
		ID=id;
		position=new Point(x,y);
		Variables.grid[y][x]=id;
		//battery=_energy;
		battery=0;
		connect=new Connect();
		if(ID<=0)
			connect.connectTo=Variables.center;
		else
			connect.connectTo=this;
		data_list=new ArrayList<DataMessage>();
		recieve_data=new ArrayList<ArrayList<DataMessage>>();
		recieve_data_new=new ArrayList<ArrayList<DataMessage>>();
		
		connect.agentsConnectToMe=new ArrayList<Integer>();
		if(ID>0)
			myColor=Color.red;
		if(ID<0)
			myColor=Color.black;
		if(ID==0)
			myColor=Color.white;
		connect.allAgentUnderMe=new ArrayList<Integer>();
	}
	
	public boolean actTimeStep()
	{
		boolean rerouting=false;
		if(battery<Variables.battery_Size)
		{
			sense();
			if(sendData())
				rerouting=true;
		}
		else
		{
			if(!sleep)
			{
				sendData();
				//sleep=true;
				//connect.connectTo=this;
			}
		}
		return rerouting;
	}
	public void nextTimeStep()
	{
		
		senseVol=0;
		//if(ID==84)
		//	System.out.println("next: "+recieve_data_new.size());
		
		for(int i=0;i<recieve_data.size();i++)
		{
			recieve_data.get(i).clear();
		}
		recieve_data.clear();
		recieve_data=recieve_data_new;
		recieve_data_new=new ArrayList<ArrayList<DataMessage>>();
		//if(ID==84)
		//	System.out.println(recieve_data.size());
		data_list=new ArrayList<DataMessage>();
		if(!sleep && battery>=Variables.battery_Size && connect.agentsConnectToMe.isEmpty() && recieve_data.isEmpty() )
		{
			connect.connectTo.removeChild(ID);
			connect.connectTo=this;
			sleep=true;
			connect.agentsConnectToMe.add(ID);
			if(!recieve_data.isEmpty())
			{
				System.out.println("merd");
				for(int i=0;i<recieve_data.size();i++)
					cntTemp+=recieve_data.get(i).size();
			}
		}
	}
	public void sense()
	{
		if(senseVol!=0)
		{
		DataMessage data=new DataMessage(senseVol,Time.time,ID);
		data_list.add(data);
		}
	}
	public boolean sendData()
	{
		//if(ID==84)
		//	System.out.println("hi"+recieve_data.size());
		for(int i=0;i<recieve_data.size();i++)
		{
		//	System.out.println("copy data from recive_data "+i+" to data_list");
		/**
		 * 	tmp_data=recieve_data.get(i);
		 *
		//	System.out.println("size of recieve data: "+tmp_data.size());
			for(int j=0;j<tmp_data.size();j++)
			{
				data_list.add(tmp_data.get(j));
		//		System.out.println("here i'm");
			}
			*/
			data_list.addAll(recieve_data.get(i));
		//	System.out.println(ID+" got data "+data_list.size());
		}
		if(!data_list.isEmpty())
		{
			
		//	System.out.println("have data to send");
		//	System.out.println("data size: "+data_list.size());
			connect.connectTo.recieveData(data_list);
			battery+=data_list.size();
		}
		if(battery>=Variables.battery_Size)//need to do rerouting
			return true;
		return false;
	}
	public void recieveData(ArrayList<DataMessage> _data_list)
	{
		
		recieve_data_new.add(_data_list);
		if(ID<0)
		{
			Manager.cntCenter+=_data_list.size();
			
			/*System.out.print(Time.time+" "+ID+" got data, size "+_data_list.size()+" : ");
			for(int j=0;j<_data_list.size();j++)
				System.out.print(_data_list.get(j).id+" ");
			System.out.println();*/
		}
	}
	public double calcConnection(Point p)
	{
		double x=Math.abs(position.x-p.x);
		double y=Math.abs(position.y-p.y);
		double d=/*Math.sqrt*/(Math.pow(x,2)+Math.pow(y,2));
		return d;
	}
	
	
	public class Connect
	{
		double totalEnergyToTransmit;
		//int connectToId;
		//Point connectToPoint;
		int numOfMessege;
		Agent connectTo=null;
		
		ArrayList<Integer> agentsConnectToMe;
		ArrayList<Integer> allAgentUnderMe;
		
		public Connect()
		{
			totalEnergyToTransmit=0;
			//connectToId=0;
			//connectToPoint=new Point(0,0);
			numOfMessege=1;
		}
	}
	public void removeChild(int id)
	{
		if(connect.agentsConnectToMe.isEmpty())
			System.out.println("ooppss");
		int i=0;
		while(connect.agentsConnectToMe.get(i)!=id)
		{	
			i++;	
		};
		connect.agentsConnectToMe.remove(i);
		i=0;
		while(connect.allAgentUnderMe.get(i)!=id)
		{	
			i++;	
		};
		connect.allAgentUnderMe.remove(i);
	}
}
